package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hospital.common.core.domain.R;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.PharmacyAssessBo;
import com.hospital.workstation.domain.vo.PharmacyAssessVo;
import com.hospital.workstation.domain.PharmacyAssess;
import com.hospital.workstation.mapper.PharmacyAssessMapper;
import com.hospital.workstation.service.IPharmacyAssessService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 入院药学评估Service业务层处理
 *
 * @author whb
 * @date 2023-02-10
 */
@RequiredArgsConstructor
@Service
public class PharmacyAssessServiceImpl implements IPharmacyAssessService {

    private final PharmacyAssessMapper baseMapper;

    /**
     * 查询入院药学评估
     */
    @Override
    public PharmacyAssessVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询入院药学评估列表
     */
    @Override
    public TableDataInfo<PharmacyAssessVo> queryPageList(PharmacyAssessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PharmacyAssess> lqw = buildQueryWrapper(bo);
        Page<PharmacyAssessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询入院药学评估列表
     */
    @Override
    public List<PharmacyAssessVo> queryList(PharmacyAssessBo bo) {
        LambdaQueryWrapper<PharmacyAssess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PharmacyAssess> buildQueryWrapper(PharmacyAssessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PharmacyAssess> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, PharmacyAssess::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getSelfCareAbilityLabel()), PharmacyAssess::getSelfCareAbilityLabel, bo.getSelfCareAbilityLabel());
        lqw.eq(bo.getSelfCareAbilityScore() != null, PharmacyAssess::getSelfCareAbilityScore, bo.getSelfCareAbilityScore());
        lqw.eq(StringUtils.isNotBlank(bo.getExpressionAbilityLabel()), PharmacyAssess::getExpressionAbilityLabel, bo.getExpressionAbilityLabel());
        lqw.eq(bo.getExpressionAbilityScore() != null, PharmacyAssess::getExpressionAbilityScore, bo.getExpressionAbilityScore());
        lqw.eq(StringUtils.isNotBlank(bo.getCareLevelLabel()), PharmacyAssess::getCareLevelLabel, bo.getCareLevelLabel());
        lqw.eq(bo.getCareLevelScore() != null, PharmacyAssess::getCareLevelScore, bo.getCareLevelScore());
        lqw.eq(bo.getGeneralConditionScore() != null, PharmacyAssess::getGeneralConditionScore, bo.getGeneralConditionScore());
        lqw.eq(StringUtils.isNotBlank(bo.getSmokeWineLabel()), PharmacyAssess::getSmokeWineLabel, bo.getSmokeWineLabel());
        lqw.eq(bo.getSmokeWineScore() != null, PharmacyAssess::getSmokeWineScore, bo.getSmokeWineScore());
        lqw.eq(StringUtils.isNotBlank(bo.getEatLabel()), PharmacyAssess::getEatLabel, bo.getEatLabel());
        lqw.eq(bo.getEatScore() != null, PharmacyAssess::getEatScore, bo.getEatScore());
        lqw.eq(StringUtils.isNotBlank(bo.getSportLabel()), PharmacyAssess::getSportLabel, bo.getSportLabel());
        lqw.eq(bo.getSportScore() != null, PharmacyAssess::getSportScore, bo.getSportScore());
        lqw.eq(bo.getLiveStyleScore() != null, PharmacyAssess::getLiveStyleScore, bo.getLiveStyleScore());
        lqw.eq(StringUtils.isNotBlank(bo.getIsForgetTakePillsLabel()), PharmacyAssess::getIsForgetTakePillsLabel, bo.getIsForgetTakePillsLabel());
        lqw.eq(bo.getIsForgetTakePillsSocre() != null, PharmacyAssess::getIsForgetTakePillsSocre, bo.getIsForgetTakePillsSocre());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReduceStopPillsLabel()), PharmacyAssess::getIsReduceStopPillsLabel, bo.getIsReduceStopPillsLabel());
        lqw.eq(bo.getIsReduceStopPillsScore() != null, PharmacyAssess::getIsReduceStopPillsScore, bo.getIsReduceStopPillsScore());
        lqw.eq(StringUtils.isNotBlank(bo.getIsForgetCarryPillsLabel()), PharmacyAssess::getIsForgetCarryPillsLabel, bo.getIsForgetCarryPillsLabel());
        lqw.eq(bo.getIsForgetCarryPillsScore() != null, PharmacyAssess::getIsForgetCarryPillsScore, bo.getIsForgetCarryPillsScore());
        lqw.eq(StringUtils.isNotBlank(bo.getIsInsistLabel()), PharmacyAssess::getIsInsistLabel, bo.getIsInsistLabel());
        lqw.eq(bo.getIsInsistScore() != null, PharmacyAssess::getIsInsistScore, bo.getIsInsistScore());
        lqw.eq(StringUtils.isNotBlank(bo.getIsTimeAmountLabel()), PharmacyAssess::getIsTimeAmountLabel, bo.getIsTimeAmountLabel());
        lqw.eq(bo.getIsTimeAmountScore() != null, PharmacyAssess::getIsTimeAmountScore, bo.getIsTimeAmountScore());
        lqw.eq(bo.getEatPillsHobbyScore() != null, PharmacyAssess::getEatPillsHobbyScore, bo.getEatPillsHobbyScore());
        lqw.eq(StringUtils.isNotBlank(bo.getIllnessKnowLabel()), PharmacyAssess::getIllnessKnowLabel, bo.getIllnessKnowLabel());
        lqw.eq(bo.getIllnessKnowScore() != null, PharmacyAssess::getIllnessKnowScore, bo.getIllnessKnowScore());
        lqw.eq(StringUtils.isNotBlank(bo.getPillsAmountLabel()), PharmacyAssess::getPillsAmountLabel, bo.getPillsAmountLabel());
        lqw.eq(bo.getPillsAmountScore() != null, PharmacyAssess::getPillsAmountScore, bo.getPillsAmountScore());
        lqw.eq(StringUtils.isNotBlank(bo.getPillsBadLabel()), PharmacyAssess::getPillsBadLabel, bo.getPillsBadLabel());
        lqw.eq(bo.getPillsBadScore() != null, PharmacyAssess::getPillsBadScore, bo.getPillsBadScore());
        lqw.eq(StringUtils.isNotBlank(bo.getPillsEffectLabel()), PharmacyAssess::getPillsEffectLabel, bo.getPillsEffectLabel());
        lqw.eq(bo.getPillsEffectScore() != null, PharmacyAssess::getPillsEffectScore, bo.getPillsEffectScore());
        lqw.eq(bo.getGeneralPillsScore() != null, PharmacyAssess::getGeneralPillsScore, bo.getGeneralPillsScore());
        lqw.eq(bo.getTotalScore() != null, PharmacyAssess::getTotalScore, bo.getTotalScore());
        return lqw;
    }

    /**
     * 保存入院药学评估
     */
    @Override
    public R<Void> insertByBo(PharmacyAssessBo bo) {
        PharmacyAssess add = BeanUtil.toBean(bo, PharmacyAssess.class);
        validEntityBeforeSave(add);
        if (bo.getId() == null) {
            // 根据病历号查询入院药学评估
            PharmacyAssess pharmacyAssess = baseMapper.selectOne(new LambdaQueryWrapper<PharmacyAssess>().eq(PharmacyAssess::getMedicalRecordNo, bo.getMedicalRecordNo()));
            // 病历号药学评估已存在
            if (ObjectUtil.isNotNull(pharmacyAssess)) {
                return R.fail("药学评估已存在，请刷新页面");
            }
            // 新增病历号的药学评估
            return baseMapper.insert(add) > 0 ? R.ok("新增成功") : R.fail("新增失败");
        } else { // 病历号药学评估已存在
            return baseMapper.updateById(add) > 0 ? R.ok("更新成功") : R.fail("更新失败");
        }
    }

    /**
     * 修改入院药学评估
     */
    @Override
    public Boolean updateByBo(PharmacyAssessBo bo) {
        PharmacyAssess update = BeanUtil.toBean(bo, PharmacyAssess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PharmacyAssess entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除入院药学评估
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据病历号查询入院药学评估信息
     *
     * @param medicalRecodeNo 病历号
     * @return
     */
    @Override
    public PharmacyAssessVo queryByMedicalRecodeNo(Integer medicalRecodeNo) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<PharmacyAssess>().eq(PharmacyAssess::getMedicalRecordNo, medicalRecodeNo));
    }
}
